package com.bangcommunity.bbframe.media.video;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author tanghc
 * @date 18/6/27
 */
public class FontImage {
    static GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    public static void main(String[] args) throws Exception {
        createImage("测试云", new Font("", Font.BOLD, 20), new File("/opt/data/a.png"), 100, 64);
        // createImage("请A1002到2号、窗口", new Font("黑体", Font.BOLD, 35), new File(
        // "/opt/data/a1.png"), 100, 64);
        // createImage("请A1001到1号窗口", new Font("黑体", Font.PLAIN, 40), new File(
        // "/opt/data/a2.png"), 100, 64);
//        createImage("测试",null,new File("/opt/data/t1.png"), 100, 64);

    }

    public static Font getCanDisplayFont(String string){
        Font[] allFonts = ge.getAllFonts();
        for(Font font: allFonts){
            if(font.canDisplayUpTo(string) == 1){
                return font;
            }
        }
        return null;
    }
    public static Point calculatePoint(int type,int outWidth,int outHeight,int width,int height){
        int x = 0,y = 0;
        if(type == 1){
            x = 10;
            y = 10;

        }else if(type == 2){
            x = outWidth - width - 10;
            y = 10;
        }else if(type == 3){
            x = 10;
            y = outHeight - height -10;
        }else if(type == 4){
            x = outWidth - width - 10;
            y = outHeight - height -10;
        }else if(type == 5){
            x = (outWidth - width)/2;
            y = (outHeight - height)/2 ;
        }
        return new Point(Math.min(x,outWidth),Math.min(y,outHeight));
    }
    // 根据str,font的样式以及输出文件目录
    public static void createImage(String str, Font font, File outFile, Integer width, Integer height)
            throws Exception {
        // 创建图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics2D g = image.createGraphics();
        // ---------- 增加下面的代码使得背景透明 -----------------
        image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g.dispose();
        g = image.createGraphics();
        // ---------- 背景透明代码结束 -----------------
        // 设置透明度
        // g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.9f)); // 1.0f为透明度
        // ，值从0-1.0，依次变得不透明

        // 画图
        g.setColor(new Color(255, 0, 0));
        g.setStroke(new BasicStroke(1));
        // g.setClip(0, 0, width, height);
        // g.setColor(Color.black);
        // g.fillRect(0, 0, width, height);// 先用黑色填充整张图片,也就是背景
        // g.setColor(Color.red);// 在换成黑色
        if(null == font){
            font = getCanDisplayFont(str);
            throw new RuntimeException("no available font");
        }
        g.setFont(font);// 设置画笔字体
        /** 用于获得垂直居中y */
        Rectangle clip = g.getClipBounds();
        FontMetrics fm = g.getFontMetrics(font);
        int ascent = fm.getAscent();
        int descent = fm.getDescent();
        // int y = (clip.height - (ascent + descent)) / 2 + ascent;
        int y = 39;
        // for (int i = 0; i < 6; i++) {// 256 340 0 680
        g.drawString(str, 10, y);// 画出字符串
        // }
        // g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();
        ImageIO.write(image, "png", outFile);// 输出png图片
    }
}
